<?php

function idTarif_manif_utilisateur($idManifestation,$statuts){
    
    $statuts= explode(';',$_SESSION['statuts']);
    $where_statuts="ts.idStatut=".$statuts[1]." ";
    for($i=2;$i<(count($statuts)-1);$i++){
        $where_statuts=$where_statuts."OR ts.idStatut=".$statuts[$i]."";    
    }
    
    $rqt = "SELECT t.idTarif as tarif_utilisateur
        FROM `tarif` AS t
        LEFT JOIN tarif_statut AS ts
        ON t.idTarif = ts.idTarif
        WHERE idManifestation=".$idManifestation." 
        AND (".$where_statuts.") 
        AND tarifHT=(SELECT MIN(t.tarifHT) as tarif_utilisateur 
        FROM `tarif` AS t 
        LEFT JOIN tarif_statut AS ts 
        ON t.idTarif = ts.idTarif 
        WHERE idManifestation=".$idManifestation." AND (".$where_statuts."))";
    //echo $rqt;
    $res = mysql_query($rqt);
    list($tarif)=  mysql_fetch_array($res);
    return $tarif;    
}

function idTarif_RDV_utilisateur($idRDV,$statuts){
    
    $statuts= explode(';',$_SESSION['statuts']);
    $where_statuts="ts.idStatut=".$statuts[1]." ";
    for($i=2;$i<(count($statuts)-1);$i++){
        $where_statuts=$where_statuts."OR ts.idStatut=".$statuts[$i]."";    
    }
   
    $rqt = "SELECT t.idTarif as tarif_utilisateur
        FROM `tarif` AS t
        LEFT JOIN tarif_statut AS ts
        ON t.idTarif = ts.idTarif
        WHERE idRDV=".$idRDV." 
        AND (".$where_statuts.") 
        AND tarifHT=(SELECT MIN(t.tarifHT) as tarif_utilisateur 
        FROM `tarif` AS t 
        LEFT JOIN tarif_statut AS ts 
        ON t.idTarif = ts.idTarif 
        WHERE idRDV=".$idRDV." AND (".$where_statuts."))";
   
    $res = mysql_query($rqt);
    list($tarif)=  mysql_fetch_array($res);
    return $tarif;    
}


function idTarif_plateforme_utilisateur($idAccesPlateforme,$statuts){
    
    $statuts= explode(';',$_SESSION['statuts']);
    $where_statuts="ts.idStatut=".$statuts[1]." ";
    for($i=2;$i<(count($statuts)-1);$i++){
        $where_statuts=$where_statuts."OR ts.idStatut=".$statuts[$i]."";    
    }
   
    $rqt = "SELECT t.idTarif as tarif_utilisateur
        FROM `tarif` AS t
        LEFT JOIN tarif_statut AS ts
        ON t.idTarif = ts.idTarif
        WHERE idAccesPlateforme=".$idAccesPlateforme." 
        AND (".$where_statuts.") 
        AND tarifHT=(SELECT MIN(t.tarifHT) as tarif_utilisateur 
        FROM `tarif` AS t 
        LEFT JOIN tarif_statut AS ts 
        ON t.idTarif = ts.idTarif 
        WHERE idAccesPlateforme=".$idAccesPlateforme." AND (".$where_statuts."))";
    $res = mysql_query($rqt) or die("Erreur : ".$rqt);
    list($tarif)=  mysql_fetch_array($res);
    return $tarif;    
}


function est_adherent($statuts){
    if (strstr($statuts, ';1;') || strstr($statuts, ';3;')){
        
        return true;        
    }else{
       
        return false;
    }
    
}


function nb_elt_commande($idCommande){
    $nb=array();
    //PARTICIPATION
    $rqt_participation="SELECT idParticipation FROM participation WHERE idCommande=".$idCommande;
    $res_participation=  mysql_query($rqt_participation);
    $nb[1]=  mysql_num_rows($res_participation);
    //RDV
    $rqt_RDV="SELECT idCommandeRDV FROM CommandeRDV WHERE idCommande=".$idCommande;
    $res_RDV=  mysql_query($rqt_RDV);
    $nb[2]=  mysql_num_rows($res_RDV);
    //ACCESPLATEFORME
    $rqt_AP="SELECT idCommandeAccesPlateforme FROM CommandeAccesPlateforme WHERE idCommande=".$idCommande;
    $res_AP=  mysql_query($rqt_AP);
    $nb[3]=  mysql_num_rows($res_AP);
    //TOTAL
    $nb[0]=$nb[1]+$nb[2]+$nb[3];
    return $nb;
}

function maj_cout_commande($idCommande){
    $totalHT_commande=0;
    $totalTVA_commande=0;
    $totalTTC_commande=0;
    
        $rqt_participation="SELECT p.idParticipation, p.idManifestation FROM participation as p LEFT JOIN manifestation as m ON p.idManifestation=m.idManifestation WHERE idCommande=".$idCommande;
       
        $res_participation=  mysql_query($rqt_participation);
        while(list($idParticipation,$idManifestation)=  mysql_fetch_array($res_participation)){
            $rqt_tarif="SELECT tarifHT FROM Tarif WHERE idTarif=".idTarif_manif_utilisateur($idManifestation, $_SESSION['statuts']);
            $res_tarif=mysql_query($rqt_tarif);            
            list($tarifHT)=  mysql_fetch_array($res_tarif);
            $tarif_tva=TVA($tarifHT,$GLOBALS['tva']);
            $tarifTTC=$tarifHT+$tarif_tva;
            $totalHT_commande+=$tarifHT;
            $totalTVA_commande+=$tarif_tva;
            $totalTTC_commande+=$tarifTTC;
            
        }
        
        
        //RDV
        
        $rqt_RDV="SELECT cr.idCommandeRDV, r.idRDV, cr.NombreRDV FROM commanderdv as cr LEFT JOIN RDV as r ON cr.idRDV=r.idRDV WHERE cr.idCommande=".$idCommande;
        //echo $rqt_RDV;
        $res_RDV=  mysql_query($rqt_RDV);
        while(list($idCommandeRDV, $idRDV, $nbRDV)=  mysql_fetch_array($res_RDV)){
            $rqt_tarif="SELECT tarifHT FROM Tarif WHERE idTarif=".  idTarif_RDV_utilisateur($idRDV, $_SESSION['statuts']);
            $res_tarif=mysql_query($rqt_tarif);
            list($tarifHT)=  mysql_fetch_array($res_tarif);
            if (isset($_SESSION['statuts']) && est_adherent($_SESSION['statuts'])){
                $tarifHT=$tarifHT*($nbRDV-1);
            }else{
                $tarifHT=$tarifHT*($nbRDV);
            }
            echo 'tva '.$GLOBALS['tva'];
            $tarif_tva=TVA($tarifHT,$GLOBALS['tva']);
            $tarifTTC=$tarifHT+$tarif_tva;
            $totalHT_commande+=$tarifHT;
            $totalTVA_commande+=$tarif_tva;
            $totalTTC_commande+=$tarifTTC;
            
        }
        
        //RDV
        
        $rqt_AP="SELECT cap.idCommandeAccesPlateforme, ap.idAccesPlateforme FROM commandeaccesplateforme as cap LEFT JOIN AccesPlateforme as ap ON cap.idAccesPlateforme=ap.idAccesPlateforme WHERE cap.idCommande=".$idCommande;
        $res_AP=  mysql_query($rqt_AP);
        while(list($idCommandeAP, $idAP)=  mysql_fetch_array($res_AP)){
            $rqt_tarif="SELECT tarifHT FROM Tarif WHERE idTarif=". idTarif_plateforme_utilisateur($idAP, $_SESSION['statuts']);
            $res_tarif=mysql_query($rqt_tarif);
            list($tarifHT)=  mysql_fetch_array($res_tarif);
            $tarifHT=$tarifHT;
            
            $tarif_tva=TVA($tarifHT,$GLOBALS['tva']);
            $tarifTTC=$tarifHT+$tarif_tva;
            $totalHT_commande+=$tarifHT;
            $totalTVA_commande+=$tarif_tva;
            $totalTTC_commande+=$tarifTTC;
            
        }
        
        $rqt_maj_tarif_cmd="UPDATE commande SET totalHT=".$totalHT_commande.", totalTTC=".$totalTTC_commande." WHERE idCommande=".$idCommande;
        mysql_query($rqt_maj_tarif_cmd);
}

?>

